package com.tle.eventexample;

import java.text.DecimalFormat;

import android.graphics.Color;
import android.os.Bundle;
import android.text.InputType;
import android.util.FloatMath;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.tle.libs.CONSTANTS;
import com.tle.libs.CONSTANTS.COLOR;
import com.tle.libs.CONSTANTS.FONT_PATHS;
import com.tle.libs.CONSTANTS.TEXT_SIZES;
import com.tle.libs.UIUtilities;

public class QuadraticEquation extends BaseActivity 
{
	////////////////////////////////////////////////////////////////////////////////////////////////////
	// Properties //
	////////////////////////////////////////////////////////////////////////////////////////////////////
	protected LinearLayout m_pnlMain;
	protected LayoutParams m_pnlMainParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
	
	protected TextView m_lblSolveQuadraticEquation;
	
	protected TextView m_lblA;
	protected EditText m_txtA;
	
	protected TextView m_lblB;
	protected EditText m_txtB;
	
	protected TextView m_lblC;
	protected EditText m_txtC;
	
	protected TextView m_lblResultTag;
	protected TextView m_lblResult;
	
	protected Button m_btnQuit;
		
	////////////////////////////////////////////////////////////////////////////////////////////////////
																					    // Properties //
	////////////////////////////////////////////////////////////////////////////////////////////////////
	
	
	
	@Override
	protected void setActivityTitle() 
	{
		// TODO Auto-generated method stub
		this.setTitle(R.string.title_QuadraticEquation);
	}
	
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		initGUI();
		registerListener();
	}
	
	
	
	
	
	////////////////////////////////////////////////////////////////////////////////////////////////////
	// Init //
	////////////////////////////////////////////////////////////////////////////////////////////////////
	
	protected void initComponents ()
	{
		m_pnlMain = new LinearLayout(this);
		
		m_lblSolveQuadraticEquation = new TextView(this);
		m_lblSolveQuadraticEquation.setText(R.string.QuadraticEquation_lblSolveQuadraticEquation);
//		m_lblSolveQuadraticEquation.setTextAlignment(TextView.TEXT_ALIGNMENT_CENTER);
		m_lblSolveQuadraticEquation.setGravity(Gravity.CENTER);
		m_lblSolveQuadraticEquation.setBackgroundColor(Color.BLACK);
		m_lblSolveQuadraticEquation.setTextColor(Color.WHITE);
		
		m_lblA = new TextView(this);
		m_lblA.setGravity(Gravity.LEFT | Gravity.BOTTOM);
		m_lblA.setBackgroundColor(Color.CYAN);
		m_lblA.setText(R.string.QuadraticEquation_lblA);
		
		m_lblB = new TextView(this);
		m_lblB.setGravity(Gravity.LEFT | Gravity.BOTTOM);
		m_lblB.setBackgroundColor(Color.CYAN);
		m_lblB.setText(R.string.QuadraticEquation_lblB);
		
		m_lblC = new TextView(this);
		m_lblC.setGravity(Gravity.LEFT | Gravity.BOTTOM);
		m_lblC.setBackgroundColor(Color.CYAN);
		m_lblC.setText(R.string.QuadraticEquation_lblC);
		
		m_lblResultTag = new TextView(this);
		m_lblResultTag.setGravity(Gravity.LEFT | Gravity.BOTTOM);
		m_lblResultTag.setBackgroundColor(Color.MAGENTA);
		m_lblResultTag.setText(R.string.QuadraticEquation_lblResultTag);
		
		m_lblResult = new TextView(this);
		m_lblResult.setGravity(Gravity.LEFT | Gravity.BOTTOM);
		m_lblResult.setBackgroundColor(COLOR.LIGHT_GREEN.getColor());
				
		m_txtA = new EditText(this);
		m_txtA.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED);
//		m_txtA.setBackgroundColor(COLOR.LIGHT_GREEN.getColor());
		
		m_txtB = new EditText(this);
		m_txtB.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED);
//		m_txtB.setBackgroundColor(COLOR.LIGHT_GREEN.getColor());
		
		m_txtC = new EditText(this);
		m_txtC.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED);
//		m_txtC.setBackgroundColor(COLOR.LIGHT_GREEN.getColor());
		
		m_btnQuit = new Button(this);
		m_btnQuit.setText(R.string.const_btnComeBack);
	}
	
	protected void initGUI ()
	{
		this.initComponents();
		
		m_pnlMain.setOrientation(LinearLayout.VERTICAL);
		this.setContentView(m_pnlMain);
		
		
		
		m_pnlMainParams.leftMargin = m_pnlMainParams.rightMargin = CONSTANTS.MARGINS.M5.getValue();
		m_pnlMainParams.gravity = Gravity.CENTER_HORIZONTAL;
		
		
		
		m_pnlMain.addView(m_lblSolveQuadraticEquation);
		m_pnlMain.addView(this.buildInputPanel(), m_pnlMainParams);
		m_pnlMain.addView(m_btnQuit);
		
		
		
		UIUtilities.setUIStandard(m_pnlMain, FONT_PATHS.PLAINT, TEXT_SIZES.NORMAL);
		UIUtilities.showSoftKeyboard(this, m_txtA);
		// Customize font size below
	}
	
	private TableLayout buildInputPanel ()
	{
		TableLayout pnlRes = new TableLayout(this);
		
		TableRow.LayoutParams col1Params = new TableRow.LayoutParams();
		col1Params.column = 0;
		col1Params.weight = 0.1f;
		
		TableRow.LayoutParams col2Params = new TableRow.LayoutParams();
		col2Params.column = 1;
		col2Params.weight = 0.9f;
		
		TableRow row = new TableRow(this);
		row.addView(m_lblA, col1Params);
		row.addView(m_txtA, col2Params);
		pnlRes.addView(row);
		
		row = new TableRow(this);
		row.addView(m_lblB, col1Params);
		row.addView(m_txtB, col2Params);
		pnlRes.addView(row);
		
		row = new TableRow(this);
		row.addView(m_lblC, col1Params);
		row.addView(m_txtC, col2Params);
		pnlRes.addView(row);
		
		row = new TableRow(this);
		row.addView(m_lblResultTag, col1Params);
		row.addView(m_lblResult, col2Params);
		pnlRes.addView(row);
		
		return pnlRes;
	}
	
	
	
	protected void registerListener ()
	{
		ButtonEvent buttonEvent = new ButtonEvent();
		m_btnQuit.setOnClickListener(buttonEvent);
		
		KeyHandler keyHandler = new KeyHandler();
		m_txtA.setOnKeyListener(keyHandler);
		m_txtB.setOnKeyListener(keyHandler);
		m_txtC.setOnKeyListener(keyHandler);
	}
	
	////////////////////////////////////////////////////////////////////////////////////////////////////
																							  // Init //
	////////////////////////////////////////////////////////////////////////////////////////////////////
	
	
	
	
	
	////////////////////////////////////////////////////////////////////////////////////////////////////
	// Support class and methods //
	////////////////////////////////////////////////////////////////////////////////////////////////////
	private class ButtonEvent implements OnClickListener
	{
		@Override
		public void onClick(View v) 
		{
			if (v == m_btnQuit)
				finish();
			
		}// end onClick
		
	}
	
	
	
	
	
	private class KeyHandler implements OnKeyListener
	{
		@Override
		public boolean onKey(View v, int keyCode, KeyEvent event)
		{
			if (event.getAction() == KeyEvent.ACTION_UP)
			{
				if (v == m_txtA || v == m_txtB || v == m_txtC)
					calculate();
			}
			
			return false;
		}// end onKey
		
	}
	
	protected void calculate ()
	{
		if (this.checkValidData() == false)
			return;
		
		Float fltA = Float.valueOf( m_txtA.getText().toString() );
		Float fltB = Float.valueOf( m_txtB.getText().toString() );
		Float fltC = Float.valueOf( m_txtC.getText().toString() );
		
		Float fltResult1 = Float.MIN_VALUE;
		Float fltResult2 = Float.MIN_VALUE;
		
		DecimalFormat decimalFormat = new DecimalFormat();
		decimalFormat.setMinimumFractionDigits(0);
		decimalFormat.setMaximumFractionDigits(2);
		
		if (fltA == 0.0f)
		{
			fltResult1 = -fltC / fltB;
			m_lblResult.setText( "x = " + decimalFormat.format(fltResult1) );
		}
		else
		{
			// Get delta
			Float fltDelta = FloatMath.pow(fltB, 2.0f) - 4.0f * fltA * fltC;
			
			if (fltDelta < 0.0f)
			{
				fltResult1 = Float.MIN_VALUE;
				m_lblResult.setText(R.string.QuadraticEquation_lblResultNoSolutions);
			}
			
			else if (fltDelta == 0.0f)
			{
				fltResult1 = -fltB / (2*fltA);
				m_lblResult.setText( R.string.QuadraticEquation_lblResultDoubleRoot + " x = " + decimalFormat.format(fltResult1));
			}
			
			else // > 0.0f
			{
				fltResult1 = (-fltB + FloatMath.sqrt(fltDelta) ) / (2 * fltA);
				fltResult2 = (-fltB - FloatMath.sqrt(fltDelta) ) / (2 * fltA);
				
				String sRes = "x1 = " + decimalFormat.format(fltResult1) + "\nx2 = " + decimalFormat.format(fltResult2);
				m_lblResult.setText(sRes);
			}
			
		}
				
	}
	
	private boolean checkValidData ()
	{
		if (m_txtA.getText().toString().isEmpty() == true || 
			m_txtB.getText().toString().isEmpty() == true ||
			m_txtC.getText().toString().isEmpty() == true)
			return false;
		
		Float fltA = Float.valueOf( m_txtA.getText().toString() );
		Float fltB = Float.valueOf( m_txtB.getText().toString() );
		
		if (fltA == 0.0f && fltB == 0.0f)
			return false;
		
		return true;
	}
	
	////////////////////////////////////////////////////////////////////////////////////////////////////
																		 // Support class and methods //
	////////////////////////////////////////////////////////////////////////////////////////////////////
}
